package com.panopset;


import java.awt.Point;
import java.util.ArrayList;
import java.util.List;

/**
 * Manage user input.
 *
 * @author Karl Dinwiddie
 *
 */
public class InputState {

    private final List<Listener> ls = new ArrayList<Listener>();

    /**
     * Register an InputState.Listener.
     * @param listener InputState.Listener.
     */
    public void register(final Listener listener) {
        if (listener == null) {
            return;
        }
        if (!ls.contains(listener)) {
            ls.add(listener);
        }
    }

    /**
     * Remove an InputState.Listener.
     * @param listener InputState.Listener.
     */
    public void remove(final Listener listener) {
        if (listener == null) {
            return;
        }
        ls.remove(listener);
    }

    public static interface Listener {
        public void reportMouseDragged(Point point);
        public void reportMouseMoved(Point point);
        public void reportMouseClicked(Point point);
        public void reportKeyPressed(int keyCode);
        public void reportKeyReleased(int keyCode);
    }

    public static class ListenerAdaptor implements Listener {
        public void reportMouseDragged(Point point){ }
        public void reportMouseMoved(Point point){ }
        public void reportMouseClicked(Point point){ }
        public void reportKeyPressed(int keyCode){ }
        public void reportKeyReleased(int keyCode){ }
    }

    public void reportMouseDragged(Point point) {
        for (Listener l : ls) {
            l.reportMouseDragged(point);
        }
    }

    public void reportMouseMoved(Point point) {
        for (Listener l : ls) {
            l.reportMouseMoved(point);
        }
    }

    public void reportMouseClicked(Point point) {
        for (Listener l : ls) {
            l.reportMouseClicked(point);
        }
    }

    public void reportKeyPressed(int keyCode) {
        for (Listener l : ls) {
            l.reportKeyPressed(keyCode);
        }
    }

    public void reportKeyReleased(int keyCode) {
        for (Listener l : ls) {
            l.reportKeyReleased(keyCode);
        }
    }

}
